﻿/*
///    This file is part of BannerLib4Net.
///
///    BannerLib4Net is free software: you can redistribute it and/or modify
///    it under the terms of the GNU General Public License as published by
///    the Free Software Foundation, either version 3 of the License, or
///    (at your option) any later version.

///    BannerLib4Net is distributed in the hope that it will be useful,
///    but WITHOUT ANY WARRANTY; without even the implied warranty of
///    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
///    GNU General Public License for more details.
///
///    You should have received a copy of the GNU General Public License
///    along with BannerLib4Net.  If not, see <http://www.gnu.org/licenses/>.
*/


using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

using BannerLib4Net.Data.Models;

namespace BannerLib4Net.Data.Extensions
{
    public static class DataReaderExtensions
    {

        public static bool HasColumn(this IDataReader rdr, string columnName)
        {
            for (int i = 0; i < rdr.FieldCount; i++)
            {
                if (rdr.GetName(i).Equals(columnName, StringComparison.InvariantCultureIgnoreCase))
                    return true;
            }
            return false;
        }

        public static T BindModelBaseProperties<T>(this IDataReader rdr, T model) where T: IModel
        {
            if (rdr.HasColumn("ID") && rdr["ID"] != DBNull.Value)
            {
                model.DatabaseKey = Int32.Parse(rdr["ID"].ToString());
            }

            if (rdr.HasColumn("NAME") && rdr["NAME"] != DBNull.Value)
            {
                model.Name = rdr["NAME"].ToString();
            }

            if (rdr.HasColumn("SORT_ORDER") && rdr["SORT_ORDER"] != DBNull.Value)
            {
                model.SortOrder = Int32.Parse(rdr["SORT_ORDER"].ToString());
            }

            if (rdr.HasColumn("ACTIVE") && rdr["ACTIVE"] != DBNull.Value)
            {
                model.Active = Boolean.Parse(rdr["ACTIVE"].ToString());
            }

            if (rdr.HasColumn("CREATED_BY") && rdr["CREATED_BY"] != DBNull.Value)
            {
                model.CreatedBy = rdr["CREATED_BY"].ToString();
            }

            if (rdr.HasColumn("EDITED_BY") && rdr["EDITED_BY"] != DBNull.Value)
            {
                model.EditedBy = rdr["EDITED_BY"].ToString();
            }

            if (rdr.HasColumn("DATE_CREATED") && rdr["DATE_CREATED"] != DBNull.Value)
            {
                model.DateCreated =  DateTime.Parse(rdr["DATE_CREATED"].ToString());
            }

            if (rdr.HasColumn("DATE_EDITED") && rdr["DATE_EDITED"] != DBNull.Value)
            {
                model.DateEdited = DateTime.Parse(rdr["DATE_EDITED"].ToString());
            }

            if (rdr.HasColumn("DESCRIPTION") && rdr["DESCRIPTION"] != DBNull.Value)
            {
                model.Description = rdr["DESCRIPTION"].ToString();
            }

            if (rdr.HasColumn("NOTES") && rdr["NOTES"] != DBNull.Value)
            {
                model.Notes = rdr["NOTES"].ToString();
            }

            return model;
        }

        public static T BindLookupModelBaseProperties<T>(this IDataReader rdr, T model) where T: LookupModel
        {
            if (rdr.HasColumn("ID") && rdr["ID"] != DBNull.Value)
            {
                model.DatabaseKey = Int32.Parse(rdr["ID"].ToString());
            }

            if (rdr.HasColumn("NAME") && rdr["NAME"] != DBNull.Value)
            {
                model.Name = rdr["NAME"].ToString();
            }

            if (rdr.HasColumn("SORT_ORDER") && rdr["SORT_ORDER"] != DBNull.Value)
            {
                model.SortOrder = Int32.Parse(rdr["SORT_ORDER"].ToString());
            }

            return model;
        }

        public static String ReadString(this IDataReader rdr, String columnName)
        {
            String retval = null;

            if(rdr[columnName] != DBNull.Value)
            {
                retval = rdr[columnName].ToString();
            }

            return retval;
        }


        public static Boolean ReadBoolean(this IDataReader rdr, String columnName)
        {
            Boolean retval = false;

            if (rdr[columnName] != DBNull.Value)
            {
                retval = Boolean.Parse(rdr[columnName].ToString());
            }

            return retval;
        }


        public static Int16 ReadInt16(this IDataReader rdr, String columnName)
        {
            Int16 retval = Int16.MinValue;

            if (rdr[columnName] != DBNull.Value)
            {
                retval = Int16.Parse(rdr[columnName].ToString());
            }

            return retval;
        }


        public static Int32 ReadInt32(this IDataReader rdr, String columnName)
        {
            Int32 retval = Int32.MinValue;

            if (rdr[columnName] != DBNull.Value)
            {
                retval = Int32.Parse(rdr[columnName].ToString());
            }

            return retval;
        }

        public static Int64 ReadInt64(this IDataReader rdr, String columnName)
        {
            Int64 retval = Int64.MinValue;

            if (rdr[columnName] != DBNull.Value)
            {
                retval = Int64.Parse(rdr[columnName].ToString());
            }

            return retval;
        }

        public static DateTime ReadDateTime(this IDataReader rdr, String columnName)
        {
            DateTime retval = DateTime.MinValue;

            if (rdr[columnName] != DBNull.Value)
            {
                retval = DateTime.Parse(rdr[columnName].ToString());
            }

            return retval;
        }

        public static Double ReadDouble(this IDataReader rdr, String columnName)
        {
            Double retval = Double.MinValue;

            if (rdr[columnName] != DBNull.Value)
            {
                retval = Double.Parse(rdr[columnName].ToString());
            }

            return retval;
        }

        public static Decimal ReadDecimal(this IDataReader rdr, String columnName)
        {
            Decimal retval = Decimal.MinValue;

            if (rdr[columnName] != DBNull.Value)
            {
                retval = Decimal.Parse(rdr[columnName].ToString());
            }

            return retval;
        }

    }
}
